﻿using System;
using System.Web;
using System.Web.Mvc;

namespace KWatkins.FluentMoqBuilder
{
	internal sealed class ControllerContextBuilder : Builder<ControllerContext>, IControllerContextBuilder
	{
		#region Private Fields
		private IHttpContextBaseBuilder httpContextBuilder;
		#endregion

		#region Instance Constructors
		internal ControllerContextBuilder() : base(new ControllerContext())
		{
			httpContextBuilder = new HttpContextBaseBuilder();
			Object.HttpContext = httpContextBuilder.Object;
		}
		#endregion

		#region Private Properties
		private IHttpContextBaseBuilder HttpContextBuilder
		{
			get
			{
				if (httpContextBuilder == null)
				{
					throw new InvalidOperationException("Cannot setup anything on Controller.HttpContext once WithHttpContext has been called.");
				}
				return httpContextBuilder;
			}
		}
		#endregion

		#region WithController
		public IControllerContextBuilder WithController(Controller controller)
		{
			Object.Controller = controller;
			return this;
		}
		#endregion

		#region WithHttpContext
		public IControllerContextBuilder WithHttpContext(HttpContextBase httpContext)
		{
			Validate.Argument(httpContext, "httpContext").IsNotNull();
			httpContextBuilder = null;
			Object.HttpContext = httpContext;
			return this;
		}
		#endregion

		#region WithRequest
		public IControllerContextBuilder WithRequest(HttpRequestBase request)
		{
			HttpContextBuilder.WithRequest(request);
			return this;
		}
		#endregion

		#region WithUrl
		public IControllerContextBuilder WithUrl(string url)
		{
			HttpContextBuilder.WithUrl(url);
			return this;
		}

		public IControllerContextBuilder WithUrl(Uri url)
		{
			HttpContextBuilder.WithUrl(url);
			return this;
		}
		#endregion

		#region WithUrlReferrer
		public IControllerContextBuilder WithUrlReferrer(string urlReferrer)
		{
			HttpContextBuilder.WithUrlReferrer(urlReferrer);
			return this;
		}
		public IControllerContextBuilder WithUrlReferrer(Uri urlReferrer)
		{
			HttpContextBuilder.WithUrlReferrer(urlReferrer);
			return this;
		}
		#endregion

		#region WithApplicationPath
		public IControllerContextBuilder WithApplicationPath(string applicationPath)
		{
			HttpContextBuilder.WithApplicationPath(applicationPath);
			return this;
		}
		#endregion

		#region WithPhysicalApplicationPath
		public IControllerContextBuilder WithPhysicalApplicationPath(string physicalApplicationPath)
		{
			HttpContextBuilder.WithPhysicalApplicationPath(physicalApplicationPath);
			return this;
		}
		#endregion
	}
}
